/*
 * @Autor: Ruohong Yu
 * @Date: 2023-04-18 21:44:09
 * @Description: file content
 * @FilePath: \ebookd:\program\vsc\ebook\node-ebook\utils\CryptoForAes.js
 */
// var crypto = require('crypto');
// //加密
// function encrypt(str, secret) {
//   secret=secret||'ybookreader'
//   var cipher = crypto.createCipher('pk', secret);
//   var enc = cipher.update(str, 'utf8', 'hex');
//   enc += cipher.final('hex');
//   return enc;
// }
// //解密
// function decrypt(str, secret) {
//   secret=secret||'ybookreader'
//   var decipher = crypto.createDecipher('aes192', secret);
//   var dec = decipher.update(str, 'hex', 'utf8');
//   dec += decipher.final('utf8');
//   return dec;
// }

const CryptoJS = require('crypto-js')
//加密
function encrypt (word) {
    let keyStr = 'ybookreaderforme'
    const key = CryptoJS.enc.Utf8.parse(keyStr)
    const srcs = CryptoJS.enc.Utf8.parse(word)
    const encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
    return encrypted.toString()
  }
  // 解密
function decrypt (word) {
    let keyStr = 'ybookreaderforme'
    const key = CryptoJS.enc.Utf8.parse(keyStr)
    const decrypt = CryptoJS.AES.decrypt(word, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
    return CryptoJS.enc.Utf8.stringify(decrypt).toString()
  }

  module.exports={
    encrypt,
    decrypt
  }

